import type { StudentWordShorthandStudyWordItem, StudyWordParams } from '@/api/types/student'
import type { WordWithStatus } from '@/pages/wordStudy/type'
import type { Ref } from 'vue'
import { formatDate } from '@/utils/format'
import { ref } from 'vue'

export function useStudyParams(words: Ref<WordWithStatus[]>) {
  const studyParams = ref<StudyWordParams>({
    studentId: 0,
    studyRecordId: 0,
    startTime: '',
    endTime: '',
    duration: 0,
    words: [],
  })

  const initStartTime = () => {
    studyParams.value.startTime = formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss')
  }

  const setWords = (words: StudentWordShorthandStudyWordItem[]) => {
    studyParams.value.words = words
  }

  const setStudyParams = (params: Partial<StudyWordParams>) => {
    studyParams.value = {
      ...studyParams.value,
      ...params,
    }
  }

  const getStudyParams = () => {
    studyParams.value.words = words.value.map(w => ({
      wordId: w.wordId,
      clickNum: w.clickNum || 0,
      correctNum: w.correctNum || 0,
      wrongNum: w.wrongNum || 0,
    }))
    return {
      ...studyParams.value,
      endTime: formatDate(new Date(), 'YYYY-MM-DD HH:mm:ss'),
    }
  }

  return { initStartTime, setWords, getStudyParams, setStudyParams }
}
